package com.arch.dev.server.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import com.arch.dev.client.vo.BudgetVO;
import com.arch.dev.client.vo.CatagoryVO;

public class FileUploadUtil {


	public static ArrayList<BudgetVO> ProcessExcelData(File file) throws InvalidFormatException,
			IOException {
		
		ArrayList<BudgetVO> budgetVOs = new ArrayList<BudgetVO>();
		InputStream inp = new FileInputStream(
				file);
		Workbook wb = WorkbookFactory.create(inp);
		Sheet sheet = wb.getSheetAt(0);
		int count = 0;
		for (Iterator<Row> rit = sheet.rowIterator(); rit.hasNext(); count++) {
			Row row = rit.next();
			if (count == 0) {
				continue;
			}
			BudgetVO budgetVO = new BudgetVO();
			CatagoryVO catagoryVO = new CatagoryVO();
			
			budgetVO.setTitle(row.getCell(0).getStringCellValue());
			catagoryVO.setName(row.getCell(1).getStringCellValue());
			budgetVO.setCost(BigDecimal.valueOf(row.getCell(2).getNumericCellValue()));
			budgetVO.setDate(row.getCell(3).getDateCellValue());
			budgetVO.setCatagoryVO(catagoryVO);
			budgetVOs.add(budgetVO);
		/*	for (Iterator<Cell> cit = row.cellIterator(); cit.hasNext();) {
				Cell cell = cit.next();				
				
				
				
				switch (cell.getCellType()) {
				case Cell.CELL_TYPE_STRING:
					System.out.println(cell.getStringCellValue());
					break;
				case Cell.CELL_TYPE_NUMERIC:
					if (DateUtil.isCellDateFormatted(cell)) {
						System.out.println(cell.getDateCellValue());
					} else {
						System.out.println(cell.getNumericCellValue());
					}

					break;

				}
			}*/
				

			
			
			
		}

		
		return budgetVOs;
	}
	
	
}
